<?php

namespace App\Admin\Controllers\Security;

use App\Models\CommonRegion;
use App\Models\SecurityCar;
use App\Models\SecurityUser;
use App\Models\User;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '安保人员信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SecurityUser());

        $grid->column('id', __('Id'));
        $grid->column('name', '安保人姓名');
        $grid->column('phone', '联系方式');
        $grid->column('password', '登录密码');
        $grid->column('province_id', ___('table.zy_security_car.Province id'))->display(function ($id){
            try{
                return CommonRegion::find($id)->name;
            }catch (\Exception $exception){
                return '---';
            }
        });
        $grid->column('city_id', ___('table.zy_security_car.City id'))->display(function ($id){
            try{
                return CommonRegion::find($id)->name;
            }catch (\Exception $exception){
                return '---';
            }
        });
        $grid->column('district_id', ___('table.zy_security_car.District id'))->display(function ($id){
            try{
                return CommonRegion::find($id)->name;
            }catch (\Exception $exception){
                return '---';
            }
        });
        $grid->column('car_id', '关联车辆')->display(function ($id){
            return SecurityCar::find($id)->license_plate;
        })->expand(function ($model) {
            $comments = $model->car()->get()->map(function ($comment) {
                return $comment->only(['id', 'name', 'type']);
            });
            $result = $comments->toArray();
            foreach ($result as $index=>$items){
                //var_dump($items['type']);exit;
                $result[$index]['type'] = SecurityCar::$type[$items['type']];
            }
            return new Table(['ID', '车主', '车辆类型'],  $result);
        });


        $grid->column('status', '当前状态')->radio(SecurityUser::$status);
        $grid->column('position', '坐标信息');
        $grid->column('login_time', '登录时间');
        //$grid->column('create_time', __('created_at'));
        //$grid->column('update_time',  __('updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SecurityUser::findOrFail($id));

        $show->field('id', ___('table.zy_security_user.Id'));
        $show->field('name', '安保人姓名');
        $show->field('phone', '联系方式');
        $show->field('password','登陆密码')->as(function (){
            return "***";
        });
        $show->field('province_id', '省份')->as(function ($id){
            return CommonRegion::find($id)->name;
        });
        $show->field('city_id','城市')->as(function ($id){
            return CommonRegion::find($id)->name;
        });
        $show->field('district_id', '地区')->as(function ($id){
            return CommonRegion::find($id)->name;
        });
        $show->field('car_id', '关联车辆')->as(function ($id){
            return SecurityCar::find($id)->license_plate;
        });;
        $show->field('status', '当前状态')->as(function ($id){
            return SecurityUser::$status[$id];
        });
        //$show->field('position', '坐标信息');
        $show->field('坐标信息')->latlong('latitude', 'longitude', $height = 400, $zoom = 16);
        $show->field('login_time', '上次登入时间');
        //$show->field('create_time', __('created_at'));
        //$show->field('update_time', __('updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SecurityUser());

        $form->text('name', '安保人姓名');

        $form->mobile('phone', '联系方式');
        $form->password('password','登陆密码');
        $form->select('province_id', ___('table.zy_security_car.Province id'))
            ->options('/admin/ajax/region?q=0')
            ->load('city_id','/admin/ajax/region');
        $form->select('city_id', ___('table.zy_security_car.City id'))
            ->load('district_id','/admin/ajax/region');
        $form->select('district_id', ___('table.zy_security_car.District id'))
            ->load('car_id','/admin/ajax/secureCar');
        $form->select('car_id', ___('安保汽车'));
        $form->radio('status', '当前状态')->options(SecurityUser::$status);

        $form->latlong('latitude', 'longitude', '当前坐标')->default(['lat' => 39.915102, 'lng' => 116.40435]);
        $form->hidden('position');
        #$form->number('login_time', '上次登入时间');
        #$form->number('create_time', __('created_at'));
        #$form->number('update_time', __('updated_at'));

        return $form;
    }

    /**
     * 动态联动select
     * @param Request $request
     * @return array
     */
    public function load(Request $request){
        $provinceId = $request->get('q');

        $result =  User::Area()->where('name', 'like',$provinceId)->get(['id', DB::raw('name as text')]);
        return  count($result)?$result:[['id'=>-1, 'text'=>'No Result']];
    }
}
